package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasKitPrintTemplateEntity;
import com.xbongbong.paas.print.model.PaasKitPrintTemplateModel;
import com.xbongbong.paas.service.PaasKitPrintService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.pro.enums.PrintSceneTypeEnum;
import com.xbongbong.pro.enums.errorcodes.PrintErrorCodeEnum;
import com.xbongbong.pro.print.pojo.pojo.dto.PaasKitPrintListDTO;
import com.xbongbong.pro.print.pojo.pojo.vo.PaasKitPrintListVO;
import com.xbongbong.pro.print.pojo.pojo.vo.PaasPrintItemVO;
import com.xbongbong.saas.constant.BasicConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 套打serviceImpl
 *
 * @author hongxiao
 * @date 2021-01-18 16:30
 * @since
 * @version
 */
@Service("paasKitPrintService")
public class PaasKitPrintServiceImpl implements PaasKitPrintService {

    private static final Logger LOG = LoggerFactory.getLogger(PaasKitPrintServiceImpl.class);

    @Resource
    private PaasKitPrintTemplateModel paasKitPrintTemplateModel;

    @Override
    public PaasKitPrintListVO list(PaasKitPrintListDTO paasKitPrintListDTO) throws XbbException {
        PaasKitPrintListVO paasKitPrintListVO = new PaasKitPrintListVO();
        PrintSceneTypeEnum printSceneTypeEnum = PrintSceneTypeEnum.getByCode(paasKitPrintListDTO.getSceneType());
        if (Objects.isNull(printSceneTypeEnum)) {
            // 套打类型不存在
            throw new XbbException(PrintErrorCodeEnum.API_ERROR_2320017);
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("type", paasKitPrintListDTO.getSceneType());
        param.put("enable", BasicConstant.IS_USE);
        List<PaasKitPrintTemplateEntity> list = paasKitPrintTemplateModel.list(param);

        List<PaasPrintItemVO> kitPrintList = new ArrayList<>();
        for (PaasKitPrintTemplateEntity paasKitPrintTemplateEntity : list) {
            // 判断模板是否三联单
            JSONObject configInfo = JsonHelperUtil.parseObject(paasKitPrintTemplateEntity.getConfigInfo());
            String size = configInfo.getString("size") == null ? "" : configInfo.getString("size");
            int isDivision = size.contains("division") ? 1 : 0;
            PaasPrintItemVO paasPrintItemVO = new PaasPrintItemVO();
            paasPrintItemVO.setId(paasKitPrintTemplateEntity.getId());
            paasPrintItemVO.setName(paasKitPrintTemplateEntity.getName());
            paasPrintItemVO.setIsDivision(isDivision);
            kitPrintList.add(paasPrintItemVO);
        }
        paasKitPrintListVO.setListPrintList(kitPrintList);

        return paasKitPrintListVO;
    }
}
